// pages/student/studentactivity/studentactivity.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        adata: {},
        check: true,
        sno: '',
        getchecksign: ['签到', '签退'],
    },
    async signBtn() {
        var ano = this.data.adata.ano
        var count = this.data.adata.gcount
        var atime = this.data.adata.holdtime
        var stype = this.data.adata.stype
        var sno = this.data.sno
        var check = this.data.check
        console.log(sno)
        
        if (check) {
            var sql = "SELECT pjcount,pcount FROM activity WHERE ano=" + ano
            var req = await this.sqlRequest(sql).then(res3 => {
                console.log(res3.data[0].pjcount)
                var pcount = res3.data[0].pcount
                var index = pcount.indexOf("限")
                if (index < 0) {
                    if (res3.data[0].pjcount >= parseInt(pcount.replace(/\D/g, ''))) {
                        wx.showToast({
                            title: '人数已满！',
                            icon: 'error',
                        })
                       
                        return
                    } else {
                        wx.showLoading({
                            title: '正在处理...',
                            mask: true,
                        })
                        sql = "UPDATE activity SET pjcount=pjcount+1 WHERE ano=" + ano
                        var req = this.sqlRequest(sql)
                    }
                } else {
                    sql = "UPDATE activity SET pjcount=pjcount+1 WHERE ano=" + ano
                    var req = this.sqlRequest(sql)
                }
                var sql = "INSERT INTO sa(sno,count,atime,stype,ano) VALUES('" + sno + "'," + count + ",'" + atime + "','"+stype+ "','" + ano + "')"
                var req = this.sqlRequest(sql).then(res => {
                    console.log(res.statusCode)
                    if (res.statusCode === 200) {
                        wx.showToast({
                            title: '报名成功！',
                            icon: 'success',
                        })
                        this.setData({
                            check: !check
                        })
                    } else {
                        wx.showToast({
                            title: '报名失败！',
                            icon: 'error',
                        })
                    }
                    wx.hideLoading()
                })
            })
        } else {
            var sql = "DELETE FROM sa where sno='" + this.data.sno + "' and ano=" + ano
            var req = this.sqlRequest(sql).then(res => {
                console.log(res.statusCode)
                if (res.statusCode === 200) {
                    sql = "UPDATE activity SET pjcount=pjcount-1 WHERE ano=" + ano
                    var req = this.sqlRequest(sql)
                    wx.showToast({
                        title: '已取消！',
                        icon: 'success',
                    })
                    this.setData({
                        check: !check
                    })
                } else {
                    wx.showToast({
                        title: '取消失败，请重试！',
                        icon: 'error',
                    })
                }
                wx.hideLoading()
            })
        }

    },
    async checkingBtn(e){
        console.log(e)
        var ano = this.data.adata.ano
        var sql = "SELECT signin,signout FROM activity WHERE ano="+ano
        var sign = {}
        var type = this.data.getchecksign[e.detail.value]
        console.log(sql)
        var req = await this.sqlRequest(sql).then(res=>{
            console.log(res.data[0])
            sign = res.data[0]
        })
        if(sign.signin==='Y'&&type==='签到'){
            wx.navigateTo({
                url: '/pages/checking/checking?ano='+ano+"&sign=签到",
              })
        }else if(sign.signout==='Y'&&type==='签退'){
            wx.navigateTo({
                url: '/pages/checking/checking?ano='+ano+"&sign=签退",
              })
        }else{
            wx.showToast({
              title: '负责人未开启'+type,
              icon:'none',
            })
        }
        
    },
    loadData(adata) {
        var ano = adata.ano
        var len = 0
        var sql = "SELECT ano FROM sa WHERE sno='"+this.data.sno+"' and ano=" + ano
        var req = this.sqlRequest(sql).then(res1 => {
            console.log(res1)
            len = res1.data.length
            if (len < 1) {
                this.setData({
                    check: true,
                })
            } else {
                this.setData({
                    check: false,
                })
            }
        })

    },
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        var adata = JSON.parse(options.adata)
        var student = wx.getStorageSync('student')
        this.setData({
            adata: adata,
            sno: student.user
        })
        this.loadData(adata)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        wx.setNavigationBarTitle({
            title: '活动详情',
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})